package com.xinghou.platform.server.api.controller;

import com.xinghou.platform.entity.req.ListReq;
import com.xinghou.platform.model.main.Store;
import com.xinghou.platform.model.main.WaitStore;
import com.xinghou.platform.server.api.bussiness.StoreService;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liufangyu on 2016/6/24.
 */
@Controller
public class StoreController extends BaseControl {
    @Resource
    private StoreService storeService;




    /**
     * 门店list接口
     *
     * @author liufangyu
     */
    @ResponseBody
    @RequestMapping(value = "/audit/list")
    public String list(WebRequest request, HttpServletResponse response,
                       @RequestBody(required = false) Map<String, Object> map) {
        List<Store> list = null;

        try {
            list = storeService.list(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        return response(list);
    }

    /**
     * 审核门店详情
     *
     * @author liufangyu
     */
    @ResponseBody
    @RequestMapping(value = "/audit/get")
    public String list(HttpServletRequest request, HttpServletResponse response,
                       @RequestBody(required = false) Map<String, Object> map) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        Store shop = null;

        try {
            shop = storeService.get(Integer.parseInt(map.get("id").toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        return response(shop);
    }

    /**
     * 审核门店详情
     *
     * @author liufangyu
     */
    @ResponseBody
    @RequestMapping(value = "/audit/save")
    public String save(HttpServletRequest request, HttpServletResponse response,
                       @RequestBody(required = false) Map<String, Object> map) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        int rs = 0;

        try {
            rs = storeService.save(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        return response(rs);
    }

    @ResponseBody
    @RequestMapping(value = "/addWaitStore", method = {RequestMethod.POST})
    public String addWaitStore( @RequestBody WaitStore waitstore,
                             HttpServletRequest request, HttpServletResponse response) {
        storeService.addWaitStore(waitstore);
        return response(null);
    }
    /**
     * 门店list接口
     *
     * @author liufangyu
     */
    @ResponseBody
    @RequestMapping(value = "/storeList")
    public String storeList(WebRequest request, HttpServletResponse response,
                       @RequestBody(required = false)ListReq listReq) {
        List<Store> list = null;

        try {
            list = storeService.storeList(listReq);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        return response(list);
    }

    /**
     * 编辑门店接口
     *
     * @author liufangyu
     */
    @ResponseBody
    @RequestMapping(value = "/updateStore")
    public String updateStore(WebRequest request, HttpServletResponse response,
                            @RequestBody(required = false)Store store) {

        try {
            storeService.updateStore(store);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        return response(null);
    }


    /**
     * 删除门店接口
     *
     * @author liufangyu
     */
    @ResponseBody
    @RequestMapping(value = "/deleteStore")
    public String deleteStore(WebRequest request, HttpServletResponse response,
                            @RequestBody(required = false)Store store) {

        try {
            storeService.delStore(store);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        return response(null);
    }

    /**
     * 新增门店接口
     *
     * @author liufangyu
     */
    @ResponseBody
    @RequestMapping(value = "/addStore")
    public String addStore(WebRequest request, HttpServletResponse response,
                            @RequestBody(required = false)Store store) {

        try {
            storeService.addStore(store);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        return response(null);
    }


}